import numpy as np
from numpy import array
from scipy import linalg
from datetime import datetime
from time import strftime
import random
import csv

'''
For proj march21
add a field to identify the cancer in population point shapefile

***revision of test12.py***
randomly choose the point from shapefile to add cancer
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_point_value_lists():
    data = []
    fn = points_CSV

    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        temp = int(float(record["region_id"]))
        data.append(temp)
        
    return data

def cal_cancer_case(iLen):
    #add cancer
    #print "========================Add Cancer============================="
    #incidence = [0.010,0.015,0.005,0.015,0.015,0.005,0.005,0.015]

    #[high rate, low rate, relative high rate, relative low rate]    
    incidence = [0.010, 0.016]
    
    sigma = 0.0010

    TotalPop = len(points_data)
    #print TotalPop
    #cancer = 0 #total of cancer case
    i = 0
    regCancer = np.fromfunction(generate_zero_list, (iLen,))
    
    while i < TotalPop:
        # if the point is within the relative high risk area
        #print int(points_data[i])
        #int(TP_data[int(points_data[i])])
        if points_data[i] in HighAreaID:
            iIncidence = 1
        else:
            iIncidence = 0

        #iIncidence = areaData[i][1]
        tempIncidence = np.random.normal(incidence[iIncidence], sigma, 1)
        if np.random.random() < tempIncidence[0]:
            #areaData[i][2] = 1
            regCancer[points_data[i]] = regCancer[points_data[i]] + 1  #total of cancer case
        i = i + 1
    return regCancer

def generate_zero_list(i):
    return i-i

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "======================================================="
    print "begin at " + getCurTime()
    #inputs = get_inputs()
    
    points_CSV = "C:/_DATA/CancerData/test/Feb10/1m_point_TP1000.csv"  # points with its Thessien Polygon ID

    # row 0, 1, 2, 3 are 10,000 pop; row 4, 5, 6, 7 are 20,000 pop
    HighAreaID = np.array([48, 49, 69, 70, 88, 273, 274, 326, 331, 364, 367, 368, 446,
                           150, 151, 156, 157, 785, 786, 787, 788, 789, 790, 793, 794, 795, 796, 801, 802, 803, 804,
                           24, 109, 484, 490, 491, 492, 493, 495, 499, 556, 557, 558, 559, 560, 561, 563, 567, 628,
                           16, 168, 185, 831, 844, 847, 848, 850, 851, 852, 853, 915, 916, 918, 919, 920, 923])

    '''
    HighAreaID = np.array([105, 410, 412, 413, 515, 516, 518, 520, 524, 527, 528,
                           53, 60, 291, 292, 293, 294, 295, 311,
                           19, 135, 145, 146, 609, 713,
                           170, 180, 181, 864, 865, 867, 873, 893, 897, 901, 902, 903, 982, 983,
                           48, 49, 69, 70, 88, 273, 274, 326, 331, 364, 367, 368, 446,
                           150, 151, 156, 157, 785, 786, 787, 788, 789, 790, 793, 794, 795, 796, 801, 802, 803, 804,
                           24, 109, 484, 490, 491, 492, 493, 495, 499, 556, 557, 558, 559, 560, 561, 563, 567, 628,
                           16, 168, 185, 831, 844, 847, 848, 850, 851, 852, 853, 915, 916, 918, 919, 920, 923])
    [1] 105, 410, 412, 413, 515, 516, 518, 520, 524, 527, 528
    [2] 53, 60, 291, 292, 293, 294, 295, 311
    [6] 19, 135, 145, 146, 609, 713
    [4] 170, 180, 181, 864, 865, 867, 873, 893, 897, 901, 902, 903, 982, 983
    [5] 48, 49, 69, 70, 88, 273, 274, 326, 331, 364, 367, 368, 446
    [3] 150, 151, 156, 157, 785, 786, 787, 788, 789, 790, 793, 794, 795, 796, 801, 802, 803, 804
    [7] 24, 109, 484, 490, 491, 492, 493, 495, 499, 556, 557, 558, 559, 560, 561, 563, 567, 628
    [8] 16, 168, 185, 831, 844, 847, 848, 850, 851, 852, 853, 915, 916, 918, 919, 920, 923
    '''
    points_data = build_point_value_lists()
    
    dis_reg = set(points_data)
    regCancerT = np.arange(0, len(dis_reg), 1)
    
    i = 0
    iterNum = 30
    while i < iterNum:
        print i
        i = i + 1
        random.shuffle(points_data)
        temp = cal_cancer_case(len(dis_reg))                               
        regCancerT = np.append(regCancerT, temp)
    regCancerT.shape = (iterNum + 1, -1)
    #temp = regCancerT.transpose
    temp = np.transpose(regCancerT)
    #print temp
    np.savetxt("C:/_DATA/CancerData/test/Mar21/cancer1000_16_pop_20000.csv", temp, delimiter=',')
    
    print "end at " + getCurTime()
